package com.wall.springbootinit.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.extra.mail.MailUtil;
import com.wall.springbootinit.common.BaseResponse;
import com.wall.springbootinit.common.ErrorCode;
import com.wall.springbootinit.common.ResultUtils;
import com.wall.springbootinit.exception.BusinessException;
import com.wall.springbootinit.model.dto.CaptchaDTO;
import com.wall.springbootinit.model.dto.VerifyDTO;
import com.wall.springbootinit.model.dto.user.RetrievePasswordDTO;
import com.wall.springbootinit.model.dto.user.UserLoginDTO;
import com.wall.springbootinit.model.vo.EmailCaptchaVo;
import com.wall.springbootinit.model.vo.UserVo;
import com.wall.springbootinit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;


/**
 * @author lzw
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;

    /**
     * 登录
     * @param userLoginDTO 登录请求
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public BaseResponse<String> login(@RequestBody UserLoginDTO userLoginDTO) {
        if (userLoginDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userId = userService.login(userLoginDTO);
        return ResultUtils.success(userId);
    }

    /**
     * 获取验证码
     */
    @ApiOperation("获取验证码")
    @GetMapping("/captcha")
    public BaseResponse<EmailCaptchaVo> getCaptcha(@RequestParam String email, HttpServletRequest request) {
        if (StringUtils.isAnyBlank(email) || !Validator.isEmail(email)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EmailCaptchaVo emailCaptchaVo = userService.getCaptcha(email, request);
        return ResultUtils.success(emailCaptchaVo);
    }

    /**
     * 重置密码
     */
    @ApiOperation("重置密码")
    @PostMapping("/retrievePassword")
    public BaseResponse<Boolean> retrievePassword(@RequestBody RetrievePasswordDTO retrievePasswordDTO) {
        if (retrievePasswordDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.retrievePassword(retrievePasswordDTO);
        return ResultUtils.success(result);
    }

    /**
     * 获取当前登录信息
     */
    @ApiOperation("获取当前登录信息")
    @GetMapping("/getCurrentUser")
    public BaseResponse<Object> getCurrentUser() {
        Object loginId = StpUtil.getLoginId();
        UserVo user = userService.getCurrentUser(loginId);
        return ResultUtils.success(user);
    }

    /**
     * 退出登录
     * @return 结果
     */
    @ApiOperation("退出登录")
    @DeleteMapping("/logout")
    public BaseResponse<Boolean> logout() {
        StpUtil.logout();
        return ResultUtils.success(true);
    }


}
